package org.example.service;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.example.code.Code;
import org.example.pojo.AuthInfo;
import org.example.pojo.Respond;
import org.example.util.JWTUtil;
import org.example.utils.RedisUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
@SuppressWarnings("ALL")
public class AuthService {
    private final String AUTH_MAP = "mapKey";

    private final JWTUtil jwtUtil = new JWTUtil();
    public void register(String router, String auth){
        RedisUtil.HashOps.hPut(AUTH_MAP,router,auth);
    }

    public Respond<String> doAuth(String router,String token){
        DecodedJWT decodedJWT = null;
        String info = (String)RedisUtil.HashOps.hGet(AUTH_MAP, router);

        if(info==null)
            return Respond.creatRespond(Code.NOT_REGISTER,"接口未注册");

        AuthInfo authInfo = JSON.parseObject(info, AuthInfo.class);
        authInfo.setTokenInfo(token);

        if(!authInfo.isEnable())
            return Respond.creatRespond(Code.AUTH_SUCCESS,"接口无需鉴权");

        return checkToken(authInfo);
    }

    private Respond<String> checkToken(AuthInfo authInfo){
        if(authInfo.isToken()){
            DecodedJWT decodedJWT = null;
            String token = authInfo.getTokenInfo();
            if(token==null||(decodedJWT=jwtUtil.verifierToken(token))==null)
                return Respond.creatAuthFailRespond(null,"token不存在");

            Integer level = decodedJWT.getClaim("level").asInt();
            String role = decodedJWT.getClaim("role").asString();

            if(authInfo.getAuthType()==0&&!authInfo.levelCompare(level))
                return Respond.creatAuthFailRespond(null,"权限不足o(╥﹏╥)o");
            if(authInfo.getAuthType()==1&&!authInfo.hasAuth(role))
                return Respond.creatAuthFailRespond(null,"身份不在权限范围内[○･｀Д´･ ○]");
        }

        return Respond.creatAuthSuccessRespond(null,"鉴权成功");
    }


    public Respond<HashMap<String,AuthInfo>> getAuthInfo(){
        Map<Object, Object> objectObjectMap = RedisUtil.HashOps.hGetAll(AUTH_MAP);
        Set<Map.Entry<Object, Object>> entries = objectObjectMap.entrySet();

        HashMap<String, AuthInfo> authMap = new HashMap<>();

        for(Map.Entry<Object,Object> entry:entries){
            String key = (String)entry.getKey();
            String val = (String)entry.getValue();

            AuthInfo authInfo = JSON.parseObject(val, AuthInfo.class);

            authMap.put(key,authInfo);
        }

        return Respond.creatRespond(authMap,Code.AUTH_SUCCESS,"权限列表");

    }

    public Respond<String> updateAuth(String router){
        try {
            String object = (String) RedisUtil.HashOps.hGet(AUTH_MAP, router);
            AuthInfo authInfo = JSON.parseObject(object, AuthInfo.class);
            authInfo.setEnable(!authInfo.isEnable());
            RedisUtil.HashOps.hPut(AUTH_MAP, router, JSON.toJSONString(authInfo));
        }catch (Exception e){
            return Respond.creatRespond(Code.REGISTER_FAIL,"修改异常");
        }
        return Respond.creatRespond(Code.REGISTER_SUCCESS,"修改成功");
    }

    public Respond<String> updateToken(String router){
        try {

            String object = (String) RedisUtil.HashOps.hGet(AUTH_MAP, router);
            AuthInfo authInfo = JSON.parseObject(object, AuthInfo.class);
            authInfo.setToken(!authInfo.isToken());
            RedisUtil.HashOps.hPut(AUTH_MAP, router, JSON.toJSONString(authInfo));
        }catch (Exception e){
            return Respond.creatRespond(Code.REGISTER_FAIL,"修改异常");
        }
        return Respond.creatRespond(Code.REGISTER_SUCCESS,"修改成功");
    }

    public Respond<String> updateLevel(String router,int level){
        try {

            String object = (String) RedisUtil.HashOps.hGet(AUTH_MAP, router);
            AuthInfo authInfo = JSON.parseObject(object, AuthInfo.class);
            authInfo.setLevel(level);
            RedisUtil.HashOps.hPut(AUTH_MAP, router, JSON.toJSONString(authInfo));
        }catch (Exception e){
            return Respond.creatRespond(Code.REGISTER_FAIL,"修改异常");
        }
        return Respond.creatRespond(Code.REGISTER_SUCCESS,"修改成功");
    }

    private DecodedJWT doTokenVerification(String token){

        return jwtUtil.verifierToken(token);
    }
}
